<?php
/**
 * Created by PhpStorm.
 * User: zhanghao
 * Date: 2014/12/22
 * Time: 17:35
 */
class SupplydemandModel extends Model{

    private $get_supplydemand_list_cows = 'sd_id,sd_publish,sd_title,sd_local,sd_type,sd_state,type_id,time_insert';
    private $my_supplydemand_cows       = 'a.sd_id,sd_title,sd_local,sd_type,sd_state,type_id';


    public function create_supplydemand($data)
    {
        $data['time_insert'] = time();

        $rs = M('supplydemand','','DB_MEETING')->add($data);

        if($data['sd_attachment'] != ''){
            foreach($data['sd_attachment'] as $val)
            {
                $data1['attachment_path'] = $val;
                $data1['supplydemand_id'] = $rs;
                $this->add_supplydemand_attachment($data1);
            }
        }
        return $rs;
    }

    public function add_supplydemand_attachment($data)
    {
        return M('supplydemand_attachment','','DB_MEETING')->add($data);
    }

    public function get_supplydemand_list($data)
    {

        $data['page'] = $data['page']  == null? 1:$data['page'];
        $data['count']= $data['count'] == null?10:$data['count'];

        $sql = 'select '.$this->get_supplydemand_list_cows.' from supplydemand';

        $data['company_name'] != null && $sql.=' join audience on supplydemand.sd_publish=audience.audience_id';

        $sql.=' where 1';

        $data['without_me']  !=null && $sql.=' and sd_publish != '.$data['uid'];
        $data['sd_type']     !=null && $sql.=' and sd_type='.$data['sd_type'];
        $data['state']       !=null && $sql.=' and sd_state in ('.$data['state'].')';
        $data['title']       !=null && $sql.=' and sd_title like "%'.$data['title'].'%"';
        $data['detail']      !=null && $sql.=' and sd_detail like "%'.$data['detail'].'%"';
        $data['local']       !=null && $sql.=' and sd_local like "%'.$data['local'].'%"';
        $data['company_name']!=null && $sql.=' and audience_name like "%'.$data['company_name'].'%"';
        $data['type_id']     !=null && $sql.=' and type_id in ('.$data['type_id'].')';

        $sql.= ' order by sd_id desc';
        $sql.= ' limit '.($data['page']-1)*$data['count'].','.$data['count'];
        $re = M('supplydemand','','DB_MEETING')->query($sql);

        foreach($re as $val){
            $val['company_name']= D('Audience')->get_audience_name_by_uid($val['sd_publish']);
            $val['company_name']= $val['company_name'] == false?'默认': $val['company_name'];
	        $val['is_contact']  = $this->check_contact($data['uid'],$val['sd_id']);
            $val['contact_num'] = $this->get_contact_num($val['sd_id']);
            $val['type_name']   = $this->get_type_name($val['type_id']);
            $val['supplydemand_attachment'] = $this->get_one_supplydemand_attachment($val['sd_id']);
            $val['supplydemand_attachment'] = $val['supplydemand_attachment'] == '' ? null : $val['supplydemand_attachment'];
            unset($val['sd_publish']);
            $arr[] = $val;
        }
        $val=null;unset($val);
        return $arr;
    }

    public function get_one_supplydemand_attachment($supplydemand_id){
        return M('supplydemand_attachment','','DB_MEETING')->where('supplydemand_id=%d',$supplydemand_id)->field('attachment_id,attachment_path')->getField('attachment_path');
    }

    public function get_supplydemand_attachment($supplydemand_id){
        return M('supplydemand_attachment','','DB_MEETING')->where('supplydemand_id=%d',$supplydemand_id)->field('attachment_id,attachment_path')->select();
    }

    public function get_type_name($id)
    {
        return M('supplydemand_type','','DB_MEETING')->where('type_id=%d',$id)->getField('type_name');
    }

    public function get_supplydemand_details($data)
    {
        if ($data['need_audience_info'] != 1) {
            $rs = M('supplydemand', '', 'DB_MEETING')->field('sd_detail,sd_state')->where('sd_id=' . $data['sd_id'])->find();
            $rs['supplydemand_attachment'] = $this->get_supplydemand_attachment($data['sd_id']);
            $rs['supplydemand_attachment'] = $rs['supplydemand_attachment'] == '' ? null : $rs['supplydemand_attachment'];
            return $rs;
        }else{
            $sql = sprintf('select sd_detail,sd_state,audience_name,audience_tel from supplydemand a join audience b ON a.sd_publish=b.audience_id where sd_id=%d',$data['sd_id']);
            $rs = M('supplydemand','','DB_MEETING')->query($sql);
            $rs[0]['supplydemand_attachment'] = $this->get_supplydemand_attachment($data['sd_id']);
            $rs[0]['supplydemand_attachment'] = $rs[0]['supplydemand_attachment'] == '' ? null : $rs[0]['supplydemand_attachment'];
            return $rs[0];
        }
    }

    public function check_contact($uid,$sd_id)
    {
        if(M('supplydemand_audience','','DB_MEETING')->where('audience_id='.$uid.' and sd_id='.$sd_id)->count())
            return 1;
        else
            return 0;
    }

    public function get_contact_num($sd_id)
    {
        return M('supplydemand_audience','','DB_MEETING')->where('sd_id='.$sd_id)->count();
    }

    public function contact_supplydemand($data)
    {
        return M('supplydemand_audience','','DB_MEETING')->add($data);
    }

    public function check_reg($data)
    {
        return M('supplydemand_audience','','DB_MEETING')->where('sd_id='.$data['sd_id'].' and audience_id='.$data['audience_id'])->find();
    }

    public function get_supplydemand_audience_list($data)
    {
        return M('supplydemand_audience','','DB_MEETING')->where('sd_id='.$data['sd_id'])->order('sda_id desc')->select();
    }

    public function update_supplydemand($data)
    {
        $data['time_update']=time();
        if($data['delete_image'] != ''){
            $arr = explode(',',$data['delete_image']);
            foreach($arr as $val){
                $this->del_supplydemand_attachment($val);
            }
        }
        return M('supplydemand','','DB_MEETING')->where('sd_id='.$data['sd_id'])->save($data);
    }

    public function del_supplydemand_attachment($id){
        $re = M('supplydemand_attachment','','DB_MEETING')->where('attachment_id=%d',$id)->find();
        unlink(str_replace(C('DOMAIN_NAME').__ROOT__.'/','',$re['attachment_path']));
        return M('supplydemand_attachment','','DB_MEETING')->where('attachment_id=%d',$id)->delete();
    }

    public function my_supplydemand($data)
    {
        $data['page'] = $data['page']  == null? 1:$data['page'];
        $data['count']= $data['count'] == null?10:$data['count'];

        $sql = 'select '.$this->my_supplydemand_cows.' from supplydemand as a';

        if($data['my_apply'] != null )
        {
            $sql.= ' join supplydemand_audience as b on a.sd_id = b.sd_id';
            $sql.= ' where b.audience_id='.$data['uid'];

        }
        else
            $sql.= ' where a.sd_publish='.$data['uid'];

        $data['type']     != null && $sql.= ' and a.sd_type='.$data['type'];
        $data['state']    != null && $sql.=' and a.sd_state in ('.$data['state'].')';
        $data['my_apply'] != null && $sql.= ' group by a.sd_title';

        $sql.= ' order by sd_id desc';
        $sql.= ' limit '.($data['page']-1)*$data['count'].','.$data['count'];

        $rs = M('supplydemand','','DB_MEETING')->query($sql);

        foreach($rs as &$val)
        {
            $val['supplydemand_attachment'] = $this->get_one_supplydemand_attachment($val['sd_id']);
            $val['supplydemand_attachment'] = $val['supplydemand_attachment'] == '' ? null : $val['supplydemand_attachment'];
        }

        return $rs;
    }

    public function get_supplydemand_type()
    {
        return M('supplydemand_type','','DB_MEETING')->field('type_id,type_name')->select();
    }
}
